//
//  SWStatusRetweetView.swift
//  MYSwiftWeiBo
//
//  Created by shingwai chan on 16/3/5.
//  Copyright © 2016年 ChanShingWai. All rights reserved.
//

import UIKit
import SnapKit

//被转发微博
class SWStatusRetweetView: UIView {

    //保存底部约束 重新改变约束
    var retweetBottom: Constraint?
    
    var status: SWStatus? {
        didSet {
        
            let name = status?.user?.screen_name ?? "没名"
            let text = status?.text ?? "没内容"
            //设置内容
            retweetContentLabel.text = "@\(name): \(text)"
            //设图片
            retweetBottom?.uninstall()
            if let _ = status?.storePictureURLs {//有图的时候
                retweetPictrueView.hidden = false
                retweetPictrueView.status = status
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetBottom = make.bottom.equalTo(retweetPictrueView).offset(SWStatusCellMargin).constraint
                })
            }else {//没有配图
                retweetPictrueView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetBottom = make.bottom.equalTo(retweetContentLabel).offset(SWStatusCellMargin).constraint
                })
            }
        }
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    func setUpUI() {
        addSubview(retweetBgButton)
        addSubview(retweetContentLabel)
        addSubview(retweetPictrueView)
        
        //添加约束
        //背景
        retweetBgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        //内容
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(SWStatusCellMargin)
            make.trailing.equalTo(self).offset(-SWStatusCellMargin)
        }
        //图片
        retweetPictrueView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(SWStatusCellMargin)
            make.leading.equalTo(self).offset(SWStatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        //根据子控件约束
        self.snp_makeConstraints { (make) -> Void in
           self.retweetBottom = make.bottom.equalTo(retweetPictrueView).offset(SWStatusCellMargin).constraint
        }
    
    }
    
    //MARK: 懒加载子控件
    /// 转发背景按钮
    lazy var retweetBgButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
    
        return button
    }()
    
    ///被转发内容
    lazy var retweetContentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), font: 15)
        label.numberOfLines = 0
    
        return label
    }()
    /// 被转发的微博配图
    lazy var retweetPictrueView :SWStatusPictureView = SWStatusPictureView()
    
}
